module.exports = class extends think.cmswing.rest {
  /**
   * app用户注册接口
   * 只需要传值手机号和密码
   * 验证手机号是否重复
   * @returns {Promise.<*>}
   */
  async postAction() {
    const mobile = this.post('mobile');
    const password = this.post('password');
    if (think.isEmpty(mobile)) return this.fail('用户信息不能为空');
    const list = await this.model('member').where({mobile: mobile}).count();
    if (!list == 0) {
      return this.fail('手机号码不能重复注册');
    }
    const user = {
      username: mobile, // 用户名
      password: encryptPassword(password), // 密码
      mobile: mobile, // 联系人手机
      reg_time: new Date().valueOf(), // 注册时间
      reg_ip: _ip2int(this.ip), // 注册ip
      status: 1, // 数据启用状态
      zhucestatus: 0, // 注册默认需要审核
      groupid: 11, // 身份类型 9客户/10供应商
      user_active: 0, // 是否激活
      oastatus: 0// 是否在oa注册0未注册、1正在注册、2已注册
    };
    const id = await this.model('member').add(user);
    console.log('+++++++++' + id);
    if (!id) return this.fail('用户新增失败');
    return this.success('用户新增成功');
  }

  /**
   * 登录验证
   *返回用户信息
   * @returns {Promise.<*>}
   */
  async getAction() {
    const mobile = this.get('mobile');
    const password = this.get('password');
    let count;
    let list;
    if (think.isEmpty(mobile)) return this.fail('手机号码不能为空');
    if (think.isEmpty(password)) {
      count = await this.model('member').where({status: 1, mobile: mobile}).count();
      list = await this.model('member').where({mobile: mobile}).select();
    } else {
      count = await this.model('member').where({status: 1, mobile: mobile, password: encryptPassword(password)}).count();
      list = await this.model('member').where({mobile: mobile, password: encryptPassword(password)}).select();
    }

    if (!count == 0 && !count == 1) {
      return this.fail('手机号码存在多个账户，请联系客服人员');
    }
    if (count == 0) {
      return this.fail('手机号码不存在或密码错误');
    }
    if (count == 1) {
      if (list[0].is_admin != 0) {
        return this.fail('非会员不能登录app');
      };
      return this.success(list, '登陆验证成功');
    }
  }

  async putAction() {
    if (this.id) {
      const data = this.getData();
      const updata = this.post();
      think.logger.info('API-member-putAction:id  updata', this.id, updata);
      delete updata.password;
      if (updata.avatar) {
        const avatarpath = await this.model('ext_attachment_pic').where({id: updata.avatar}).getField('path');
        if (!think.isEmpty(avatarpath)) {
          updata.avatar = avatarpath[0];
        }
      }
      const res = await this.model('member').where(data).update(updata);
      if (think.isEmpty(res)) {
        return this.fail(1, '修改信息失败', res);
      } else {
        return this.success(res, '修改信息成功');
      }
    }
    const mobile = this.post('mobile');
    const password = this.post('password');
    if (think.isEmpty(mobile)) return this.fail('手机号码不能为空');
    const count = await this.model('member').where({mobile: mobile}).count();
    const list = await this.model('member').where({mobile: mobile}).update({password: encryptPassword(password)});
    if (!count == 0 && !count == 1) {
      return this.fail('手机号码存在多个账户，请联系客服人员');
    }
    if (count == 0) {
      return this.fail('手机号码不存在或密码错误');
    }
    if (count == 1) {
      return this.success(list, '修改密码成功');
    }
  }
};
